# 1.到模块 工作簿  ---- 图的模块
from datetime import date

from openpyxl import Workbook
from openpyxl.chart import LineChart, Reference

# 2.建簿 建表
from openpyxl.chart.axis import DateAxis

wb = Workbook()
sheet = wb.active

# 添加数据
rows = [
    ['日期', '张三', '李四', '王五'],
    [date(2015,9, 1), 40, 30, 25],
    [date(2015,9, 2), 40, 25, 30],
    [date(2015,9, 3), 50, 30, 45],
    [date(2015,9, 4), 30, 25, 40],
    [date(2015,9, 5), 25, 35, 30],
    [date(2015,9, 6), 20, 40, 35],
]

for row in rows:
    sheet.append(row)

# 3.创建折线图 设置属性
c1 = LineChart()
c1.title = '销售业绩表'
c1.style = 13
c1.x_axis.title = '销售日期'
c1.y_axis.title = '百万'



# 4. 设置 折线图的数据
data = Reference(sheet, min_col=2, min_row=1,max_row=7, max_col=4)
c1.add_data(data, titles_from_data=True)

############################ 设置日期单位########################
# c1.x_axis = DateAxis(crossAx=100)
c1.x_axis.number_format = 'yyyy-mm-d'
c1.x_axis.majorTimeUnit = "days"
# 设置  X 轴上面 显示日期
dates = Reference(sheet, min_col=1, min_row=2, max_row=6)
c1.set_categories(dates)



#设置第一条折线图的样式
s1 = c1.series[0]
s1.marker.symbol = "triangle" #marker的样式
s1.marker.graphicalProperties.solidFill = "FF0000" # marker填充颜色
s1.marker.graphicalProperties.line.solidFill = "FF0000" # marker线颜色
s1.graphicalProperties.line.noFill = True  #是否填充连接线

#设置第二条折线图的样式
s2 = c1.series[1]
s2.graphicalProperties.line.solidFill = "00AAAA"
s2.graphicalProperties.line.dashStyle = "sysDot"  #破折号样式
s2.graphicalProperties.line.width = 100050 #宽度
#设置第三条折线图的样式
s2 = c1.series[2]
s2.smooth = True #是否让折现平滑



# 5.添加到表中
sheet.add_chart(c1)

# 6.保存 工作簿
wb.save('06linechart.xlsx')
